package com.example.gcmtest1;

import android.os.Bundle;
import android.os.Handler;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class AccountManagerActivity extends Activity implements AccountManagerCallback<Bundle>, View.OnClickListener {

	private String logName = "AccountManagetActivity";
	private Button cmdAuthen, cmdLogout;
	private AccountManager am = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_manager);
       
        this.cmdAuthen = (Button) this.findViewById(R.id.cmdAuthen);
        this.cmdAuthen.setOnClickListener(this);
        
        this.cmdLogout = (Button) this.findViewById(R.id.cmdLogout);
        this.cmdLogout.setOnClickListener(this);
        
        View lv1 = (View) this.getWindow().getDecorView();
        this.am = AccountManager.get(this);
        Account[] ac = am.getAccounts();
        
        Log.d(logName, "ac.length=" + ac.length);
        Account gAc = null;
        for (Account a : ac) {
        	Log.d(logName, a.name + " " + a.type);
        	if (a.type.equals("com.google")) {
        		gAc = ac[0];
        	}
        }
        
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	
    	final String AUTH = "oauth2:https://www.google.com/m8/feeds/contacts/default/full";
    	
    	if (id == 1) {
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setTitle("Select a Google account");
            final Account[] accounts = this.am.getAccountsByType("com.google");
            final int size = accounts.length;
            String[] names = new String[size];
            for (int i = 0; i < size; i++) {
              names[i] = accounts[i].name;
            }
            builder.setItems(names, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                // Stuff to do when the account is selected by the user
                Log.d(logName, "selected=" + accounts[which].name);
                doAuthenticate(accounts[which], AUTH);
              }
            });    	
        	return(builder.create());	
    	}
    	
    	return(null);
    }
    
    private void doAuthenticate(Account ac, String AUTH) {
    	final Handler handler = new Handler(); 
    	//this.am.getAuthToken(ac, AUTH, true, this, handler);
    	this.am.getAuthToken(ac, AUTH, true, this, null);

    	
    }

    @Override
	public void run(AccountManagerFuture<Bundle> arg0) {
    	try {
    		String token = arg0.getResult().getString(AccountManager.KEY_AUTHTOKEN);
    		Log.d(logName, "token=" + token);
    		
    	} catch (Exception e) {
    		Log.e(logName, e.toString());
    	}
    	
	}
    
	@Override
	public void onClick(View arg0) {
        Account[] ac = am.getAccounts();
        String AUTH = "oauth2:https://www.google.com/m8/feeds/contacts/default/full";
        
        Log.d(logName, "ac.length=" + ac.length);
        Account gAc = null;
        for (Account a : ac) {
        	Log.d(logName, a.name + " " + a.type);
        	if (a.type.equals("com.google")) {
        		gAc = ac[0];
        	}
        }
        
		if (arg0 == this.cmdAuthen) {

	        if (gAc != null) {
	        	//am.getAuthToken(gAc, AUTH, true, this, null);
	        	this.showDialog(1);
	        }
	        
		} else if (arg0 == this.cmdLogout) {
			// cannot be done
		}
		
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_account_manager, menu);
        return true;
    }

}
